import "package:flutter/material.dart";
import "package:myapp/_common/RichTextFoldWidget.dart";
import "package:myapp/_views/dynamic/widget/DynamicShareItemWidget.dart";
import "package:myapp/_views/dynamic/widget/DynamicUPRecommendGoodsWidget.dart";
import "../type.dart";
import "./DynamicImageListWidget.dart";
import "./DynamicVideoWidget.dart";
import "./DynamicArticleWidget.dart";
import "./DynamicHotCommentWidget.dart";
import "./DynamicRelevantItem.dart";

// 动态列表
class DynamicItemWidget extends StatelessWidget {
  final DynamicItem dynamicItem;
  const DynamicItemWidget({
    super.key,
    required this.dynamicItem,
  });
  @override
  Widget build(BuildContext context) {
    DynamicUPInfo dynamicUPInfo = dynamicItem.dynamicUPInfo;
    String dynmaicTip = '';
    if (dynamicItem.dynamicType == 'video' &&
        dynamicItem.dynamicVideoItem != null) {
      if (dynamicItem.dynamicVideoItem!.isCooperate) {
        dynmaicTip = '与他人联合创作';
      } else if (dynamicItem.dynamicVideoItem!.videoType == 'playback') {
        dynmaicTip = '投稿了直播回放';
      } else if (dynamicItem.dynamicVideoItem!.videoType == 'dynamic') {
        dynmaicTip = '投稿了动态视频';
      } else if (dynamicItem.dynamicVideoItem!.videoType == 'animate') {
        dynmaicTip = '更新了';
      } else {
        dynmaicTip = '投稿了视频';
      }
    } else if (dynamicItem.dynamicType == 'article' &&
        dynamicItem.dynamicArticleItem != null) {
      dynmaicTip = '投稿了专栏';
    }
    DynamicUPInfoFansCardInfo? dynamicUPInfoFansCardInfo =
        dynamicItem.dynamicUPInfo.dynamicUPInfoFansCardInfo;

    DynamicTopic? dynamicTopic = dynamicItem.dynamicTopic;
    List<DynamicLabel>? dynamicLabelList = dynamicItem.dynamicLabelList;
    String? content = dynamicItem.content;
    String dynamicType = dynamicItem.dynamicType;

    DynamicImageTextItem? dynamicImageTextItem =
        dynamicItem.dynamicImageTextItem;

    DynamicVideoItem? dynamicVideoItem = dynamicItem.dynamicVideoItem;
    DynamicArticleItem? dynamicArticleItem = dynamicItem.dynamicArticleItem;
    DynamicShareItem? dynamicShareItem = dynamicItem.dyanmicShareItem;
    Color usernameColor = dynamicUPInfo.isVIP
        ? const Color.fromARGB(255, 255, 120, 165)
        : Colors.black;

    DynamicRelevant? dynamicRelevant = dynamicItem.dynamicRelevant;
    List<DynamicUPRecommendGoods>? dynamicUPRecommendGoodsList =
        dynamicItem.dynamicUPRecommendGoodsList;
    return Container(
      margin: const EdgeInsets.only(bottom: 10),
      padding: const EdgeInsets.fromLTRB(0, 8, 0, 8),
      decoration: const BoxDecoration(
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 10, right: 10, bottom: 6),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(
                  flex: 1,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Padding(
                        padding: const EdgeInsets.only(right: 14),
                        child: Stack(
                          alignment: Alignment.bottomRight,
                          children: [
                            CircleAvatar(
                              radius: 20,
                              backgroundImage: AssetImage(dynamicUPInfo.avatar),
                              backgroundColor: Colors.black26,
                            ),
                            DynamicUPIdentifyWidget(
                              dynamicUPInfo: dynamicUPInfo,
                            ),
                          ],
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Flexible(
                                  fit: FlexFit.loose,
                                  child: Text(
                                    dynamicUPInfo.name,
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                      fontSize: 13,
                                      fontWeight: FontWeight.bold,
                                      color: usernameColor,
                                    ),
                                  ),
                                ),
                               dynamicUPInfo.isLiving? Container(
                                  margin: const EdgeInsets.only(left: 2),
                                  padding:
                                      const EdgeInsets.fromLTRB(6, 0, 6, 0),
                                  decoration: BoxDecoration(
                                    border: Border.all(
                                        width: 0.5, color: Colors.pink),
                                    borderRadius: BorderRadius.circular(10),
                                  ),
                                  child: const Text(
                                    '直播中',
                                    style: TextStyle(
                                      fontSize: 9,
                                      color: Colors.pink,
                                    ),
                                  ),
                                ):const SizedBox.shrink(),
                              ],
                            ),
                            Text(
                              "${dynamicItem.time}${dynmaicTip.isNotEmpty ? ' · $dynmaicTip' : ''}",
                              overflow: TextOverflow.ellipsis,
                              maxLines: 1,
                              style: const TextStyle(
                                color: Colors.black38,
                                fontSize: 12,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Stack(
                      alignment: Alignment.centerLeft,
                      children: [
                        dynamicUPInfoFansCardInfo == null
                            ? const SizedBox()
                            : ClipRRect(
                                borderRadius: BorderRadius.circular(14),
                                child: Image.asset(
                                  dynamicUPInfoFansCardInfo.imageUrl,
                                  fit: BoxFit.cover,
                                  height: 28,
                                  width: 80,
                                ),
                              ),
                        dynamicUPInfoFansCardInfo?.index != null
                            ? Text(
                                dynamicUPInfoFansCardInfo?.index ?? '',
                                style: TextStyle(
                                  color: Color(
                                    dynamicUPInfoFansCardInfo!.color as int,
                                  ),
                                  fontSize: 8,
                                  fontWeight: FontWeight.bold,
                                ),
                              )
                            : const SizedBox.shrink()
                      ],
                    ),
                    Container(
                      margin: const EdgeInsets.only(left: 10),
                      child: const Icon(
                        Icons.more_vert,
                        size: 20,
                        color: Colors.black54,
                      ),
                    ),
                  ],
                )
              ],
            ),
          ),
          dynamicTopic != null
              ? Padding(
                  padding: const EdgeInsets.only(
                    bottom: 1,
                    left: 10,
                    right: 10,
                  ),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.tag,
                        color: Colors.blue.shade600,
                      ),
                      Expanded(
                        flex: 1,
                        child: Text(
                          dynamicTopic.name,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            color: Colors.blue.shade800,
                            fontSize: 16,
                          ),
                        ),
                      )
                    ],
                  ),
                )
              : const SizedBox(),
          dynamicLabelList != null && dynamicLabelList.isNotEmpty
              ? Padding(
                  padding: const EdgeInsets.only(
                    bottom: 10,
                    left: 10,
                    right: 10,
                  ),
                  child: Row(
                    children: dynamicLabelList.map((e) {
                      return Text(
                        "#${e.name}#",
                        style: TextStyle(
                          color: Colors.blue.shade800,
                          fontSize: 16,
                        ),
                      );
                    }).toList(),
                  ),
                )
              : const SizedBox(),
          content != null
              ? Padding(
                  padding: const EdgeInsets.only(
                    bottom: 10,
                    left: 10,
                    right: 10,
                  ),
                  child: LayoutBuilder(
                    builder: (context, constraints) {
                      return RichTextFoldWidget(
                        content: content,
                        maxLines: 4,
                        maxWidth: constraints.maxWidth,
                        textStyle: const TextStyle(
                          height: 1.5,
                          fontSize: 17,
                          color: Colors.black,
                        ),
                        reservedSpace: 3,
                        end: const TextSpan(
                          text: '全文',
                          style: TextStyle(
                            fontSize: 17,
                            height: 1.5,
                            color: Color.fromARGB(255, 26, 113, 184),
                          ),
                        ),
                      );
                    },
                  ),
                )
              : const SizedBox(),
          dynamicType == 'imageText' && dynamicImageTextItem != null
              ? DynamicImageListWidget(
                  dynamicImageTextItem: dynamicImageTextItem,
                )
              : const SizedBox(),
          dynamicType == 'video' && dynamicVideoItem != null
              ? DynamicVideoWidget(
                  dynamicVideoItem: dynamicVideoItem,
                )
              : const SizedBox(),
          dynamicType == 'article' && dynamicArticleItem != null
              ? DynamicArticleWidget(
                  dynamicArticleItem: dynamicArticleItem,
                )
              : const SizedBox(),
          dynamicType == 'share' && dynamicShareItem != null
              ? DynamicShareItemWidget(dynamicShareItem: dynamicShareItem)
              : const SizedBox.shrink(),
          dynamicRelevant != null
              ? DynamicRelevantItemWidget(
                  dynamicRelevant: dynamicRelevant,
                )
              : const SizedBox.shrink(),
          dynamicUPRecommendGoodsList != null
              ? DynamicUPRecommendGoodsWidget(
                  dynamicUPRecommendGoodsList: dynamicUPRecommendGoodsList,
                )
              : const SizedBox.shrink(),
          dynamicItem.hotComment != null
              ? Padding(
                  padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
                  child: DynamicHotCommentWidget(
                    hotComment: dynamicItem.hotComment as DynamicCommentItem,
                  ),
                )
              : const SizedBox(),
          Padding(
            padding: const EdgeInsets.only(top: 6, bottom: 5),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(
                  flex: 1,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Icon(
                        Icons.turn_slight_right,
                        size: 18,
                        color: Colors.black54,
                      ),
                      Padding(
                        padding: const EdgeInsets.only(left: 2),
                        child: Text(
                          '${dynamicItem.shareCount > 0 ? dynamicItem.shareCount : '转发'}',
                          style: const TextStyle(
                            color: Colors.black54,
                            fontSize: 13,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Icon(
                        Icons.mode_comment_outlined,
                        size: 18,
                        color: Colors.black54,
                      ),
                      Padding(
                        padding: const EdgeInsets.only(left: 2),
                        child: Text(
                          '${dynamicItem.commentCount > 0 ? dynamicItem.commentCount : '评论'}',
                          style: const TextStyle(
                            color: Colors.black54,
                            fontSize: 13,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Icon(
                        Icons.group_work_outlined,
                        size: 18,
                        color: Colors.black54,
                      ),
                      Padding(
                        padding: const EdgeInsets.only(left: 2),
                        child: Text(
                          '${dynamicItem.likeCount > 0 ? dynamicItem.likeCount : '点赞'}',
                          style: const TextStyle(
                            color: Colors.black54,
                            fontSize: 13,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

// 用户身份
class DynamicUPIdentifyWidget extends StatelessWidget {
  final DynamicUPInfo dynamicUPInfo;
  const DynamicUPIdentifyWidget({
    super.key,
    required this.dynamicUPInfo,
  });
  @override
  Widget build(BuildContext context) {
    DynamicUPInfoAuthentication? dynamicUPInfoAuthentication =
        dynamicUPInfo.dynamicUPInfoAuthentication;
    bool isVIP = dynamicUPInfo.isVIP;
    double size = 13;
    Widget child = const SizedBox();
    Color bgColor = Colors.transparent;
    if (dynamicUPInfoAuthentication != null) {
      String type = dynamicUPInfoAuthentication.type;
      if (type == 'institution') {
        // 机构认证
        bgColor = const Color.fromARGB(255, 117, 193, 255);
        child = const Icon(
          Icons.flash_on_outlined,
          size: 10,
          color: Colors.white,
        );
      } else if (type == 'UP') {
        // up主认证
        bgColor = const Color.fromARGB(255, 255, 206, 59);
        child = const Icon(
          Icons.flash_on_outlined,
          size: 10,
          color: Colors.white,
        );
      } else {
        return const SizedBox.shrink();
      }
    } else if (isVIP) {
      bgColor = const Color.fromARGB(255, 255, 141, 179);
      child = const Text(
        '大',
        style: TextStyle(
          color: Colors.white,
          fontSize: 7,
          fontWeight: FontWeight.bold,
        ),
      );
    } else {
      return const SizedBox.shrink();
    }
    return Container(
      height: size,
      width: size,
      decoration: BoxDecoration(
        border: Border.all(width: 1, color: Colors.white),
        borderRadius: BorderRadius.circular(size / 2),
        color: bgColor,
      ),
      child: Align(
        alignment: Alignment.center,
        child: child,
      ),
    );
  }
}
